package org.calzz.example.crud;

import java.util.List;

import org.springframework.jdbc.core.simple.SimpleJdbcDaoSupport;

public class ProductDAO extends SimpleJdbcDaoSupport {

	private ProductMapper mapper = new ProductMapper();

	public ProductDTO getProductById(int productId) {
		return getSimpleJdbcTemplate().queryForObject("SELECT product_id, name, price FROM products WHERE product_id = ?", mapper, productId);
	}

	public List<ProductDTO> findProducts(String pattern) {
		return getSimpleJdbcTemplate().query("SELECT product_id, name, price FROM products WHERE name LIKE ?", mapper, pattern);
	}

	public void addProduct(ProductDTO product) {
		getSimpleJdbcTemplate().update("INSERT INTO products(product_id, name, price) VALUES (?, ?, ?)", product.getProductId(), product.getName(), product.getPrice());
	}

	public void deleteProduct(int productId) {
		getSimpleJdbcTemplate().update("DELETE FROM products WHERE product_id = ?", productId);
	}

	public void updateProduct(ProductDTO product) {
		getSimpleJdbcTemplate().update("UPDATE products SET name = ?, price = ? WHERE product_id = ?", product.getName(), product.getPrice(), product.getProductId());
	}

}
